package org.dromara.netbar.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.netbar.domain.bo.NetbarGiftPackLogBo;
import org.dromara.netbar.domain.vo.NetbarGiftPackLogVo;
import org.dromara.netbar.domain.NetbarGiftPackLog;
import org.dromara.netbar.mapper.NetbarGiftPackLogMapper;
import org.dromara.netbar.service.INetbarGiftPackLogService;

import java.util.*;

/**
 * 礼包发放记录Service业务层处理
 *
 * @author Lion Li
 * @date 2025-07-21
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class NetbarGiftPackLogServiceImpl implements INetbarGiftPackLogService {

    private final NetbarGiftPackLogMapper baseMapper;

    /**
     * 查询礼包发放记录
     *
     * @param packLogId 主键
     * @return 礼包发放记录
     */
    @Override
    public NetbarGiftPackLogVo queryById(Long packLogId){
        return baseMapper.selectVoById(packLogId);
    }

    /**
     * 分页查询礼包发放记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 礼包发放记录分页列表
     */
    @Override
    public TableDataInfo<NetbarGiftPackLogVo> queryPageList(NetbarGiftPackLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NetbarGiftPackLog> lqw = buildQueryWrapper(bo);
        Page<NetbarGiftPackLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的礼包发放记录列表
     *
     * @param bo 查询条件
     * @return 礼包发放记录列表
     */
    @Override
    public List<NetbarGiftPackLogVo> queryList(NetbarGiftPackLogBo bo) {
        LambdaQueryWrapper<NetbarGiftPackLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NetbarGiftPackLog> buildQueryWrapper(NetbarGiftPackLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NetbarGiftPackLog> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(NetbarGiftPackLog::getPackLogId);
        lqw.eq(bo.getPackId() != null, NetbarGiftPackLog::getPackId, bo.getPackId());
        lqw.eq(bo.getMemberId() != null, NetbarGiftPackLog::getMemberId, bo.getMemberId());
        lqw.eq(bo.getSendTime() != null, NetbarGiftPackLog::getSendTime, bo.getSendTime());
        return lqw;
    }

    /**
     * 新增礼包发放记录
     *
     * @param bo 礼包发放记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(NetbarGiftPackLogBo bo) {
        NetbarGiftPackLog add = MapstructUtils.convert(bo, NetbarGiftPackLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPackLogId(add.getPackLogId());
        }
        return flag;
    }

    /**
     * 修改礼包发放记录
     *
     * @param bo 礼包发放记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(NetbarGiftPackLogBo bo) {
        NetbarGiftPackLog update = MapstructUtils.convert(bo, NetbarGiftPackLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NetbarGiftPackLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除礼包发放记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public void saveSendLogs(List<Long> giftPackIdList, List<Long> memberIdList, Date sendTime) {
        if (giftPackIdList.isEmpty() || memberIdList.isEmpty()) {
            return;
        }
        NetbarGiftPackLog netbarGiftPackLog;
        List<NetbarGiftPackLog> netbarGiftPackLogs = new ArrayList<>();
        for (Long memberId : memberIdList) {
            for (Long giftPackId : giftPackIdList) {
                netbarGiftPackLog = new NetbarGiftPackLog();
                netbarGiftPackLog.setPackId(giftPackId);
                netbarGiftPackLog.setMemberId(memberId);
                netbarGiftPackLog.setSendTime(sendTime);
                netbarGiftPackLogs.add(netbarGiftPackLog);
            }
        }
        if (!netbarGiftPackLogs.isEmpty()) {
            baseMapper.insertBatch(netbarGiftPackLogs);
        }
    }
}
